package com.zenni.core;

import com.zenni.utils.DriverUtils;
import com.zenni.utils.LogUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;

/**
 * Created by douglasj on 3/1/16.
 */
public class OrderFunction {
    private WebDriver driver;
    private DriverUtils driverUtils = new DriverUtils();
    Logger logger = Logger.getLogger(this.getClass().getName());
    public OrderFunction(WebDriver driver) throws Exception{
        this.driver = driver;
    }
    public String getCurrentPageTitle() throws Exception{
        String title = driver.findElement(By.xpath(OrderVariables.orderTile_xpath)).getText();
        return title;
    }
    public void selectPrescriptionType(String prescriptiontype) throws Exception{
        logger.info("We are selecting "+prescriptiontype+" ######");
        Select prescriptiontype_menu = new Select(driver.findElement(By.id(OrderVariables.prescriptiontype_menu_id)));
        //prescriptiontype_menu.selectByValue("");
        prescriptiontype_menu.selectByValue(prescriptiontype);
    }
    public String getCurrentPrescriptionType() throws Exception{
        String prescriptiontype_value = driver.findElement(By.xpath(OrderVariables.current_prescriptiontype)).getText();
        return prescriptiontype_value;
    }
    public void selectOdSPH(String value) throws Exception{
        logger.info("Set ODSPH as "+value+" .");
        Select odSPH = new Select(driver.findElement(By.id(OrderVariables.odSPH_id)));
        odSPH.selectByValue(value);
    }
    public void selectOsSPH(String value) throws Exception{
        logger.info("Set OSSPH as "+value+" .");
        Select osSPH = new Select(driver.findElement(By.id(OrderVariables.osSPH_id)));
        osSPH.selectByValue(value);
    }
    public void selectODCYL(String value) throws Exception{
        logger.info("Set ODCYL as "+value+" .");
        Select odCYL = new Select(driver.findElement(By.id(OrderVariables.odCYL_id)));
        odCYL.selectByValue(value);
    }
    public void selectOSCYL(String value) throws Exception{
        logger.info("Set OSCYL as "+value+" .");
        Select osCYL = new Select(driver.findElement(By.id(OrderVariables.osCYL_id)));
        osCYL.selectByValue(value);
    }
    public void selectODAXIS(String value) throws Exception{
        logger.info("Set ODAXIS as"+value+" .");
        Select odAXIS = new Select(driver.findElement(By.id(OrderVariables.odAXIS_id)));
        odAXIS.selectByValue(value);
    }
    public void selectOSAXIS(String value) throws Exception{
        logger.info("Set OSAXIS as"+value+" .");
        Select osAXIS = new Select(driver.findElement(By.id(OrderVariables.osAXIS_id)));
        osAXIS.selectByValue(value);
    }
    public void selectNVADD(String value) throws Exception{
        logger.info("Set NVADD as"+value+" .");
        Select nvADD = new Select(driver.findElement(By.id(OrderVariables.nvADD_id)));
        nvADD.selectByValue(value);
    }
    public void selectPrismValuesNo() throws Exception{
        logger.info("Do not select Prism value");
        driver.findElement(By.xpath(OrderVariables.prism_Values_no_xpath)).click();
    }
    public void selectPrismValuesYes() throws Exception{
        logger.info("Select Prism value");
        driver.findElement(By.xpath(OrderVariables.prism_Values_yes_xpath)).click();
    }
    public void selectOdPrismValue(String value) throws Exception{
        logger.info("Set OdPrism as"+value+" .");
        Select odprismvalue = new Select(driver.findElement(By.id(OrderVariables.odPrismValue_id)));
        odprismvalue.selectByValue(value);
    }
    public void selectOsPrismValue(String value) throws Exception{
        logger.info("Set OsPrism as"+value+" .");
        Select osprismvalue = new Select(driver.findElement(By.id(OrderVariables.osPrismValue_id)));
        osprismvalue.selectByValue(value);
    }
    public void selectOdBaseDirection(String value) throws Exception{
        logger.info("Set Od Base Direction as"+value+" .");
        Select odbasedirection = new Select(driver.findElement(By.id(OrderVariables.odPrismDirection_id)));
        odbasedirection.selectByValue(value);
    }
    public void selectOsBaseDirection(String value) throws Exception{
        logger.info("Set Os Base Direction as"+value+" .");
        Select osbasedirection = new Select(driver.findElement(By.id(OrderVariables.osPrismDirection_id)));
        osbasedirection.selectByValue(value);
    }
    public void selectSinglePD(String value) throws Exception{
        logger.info("Set SinglePD as"+value+" .");
        driver.findElement(By.xpath(OrderVariables.single_pd_xpath)).click();
        Select singlepdmenu = new Select(driver.findElement(By.id(OrderVariables.single_pd_value_id)));
        singlepdmenu.selectByValue(value);
    }
    public void selectDuoPDLeft(String value) throws Exception{
        logger.info("Set Duo PD Left as"+value+" .");
        driver.findElement(By.xpath(OrderVariables.duo_pd_xpath)).click();
        Select duopdleftmenu = new Select(driver.findElement(By.id(OrderVariables.duo_pd_left_value_id)));
        duopdleftmenu.selectByValue(value);
    }
    public void selectDuoPDRight(String value) throws Exception{
        logger.info("Set Duo PD Right as"+value+" .");
        driver.findElement(By.xpath(OrderVariables.duo_pd_xpath)).click();
        Select duopdrightmenu = new Select(driver.findElement(By.id(OrderVariables.duo_pd_right_value_id)));
        duopdrightmenu.selectByValue(value);
    }
    public void clickprescriptionnextbutton() throws Exception{
        logger.info("Click next button");
        //driverUtils.waitTillElementExistsByXpath(driver,OrderVariables.prescription_next_button_id,3000);
        driver.findElement(By.id(OrderVariables.prescription_next_button_id)).click();
        Thread thread = new Thread();
        thread.sleep(3000);
        //driverUtils.waitTillElementExistsByXpath(driver,OrderVariables.warn_message_pop_up_title_xpath,5000);
    }
    public String getCurrentLensTitle() throws Exception{
        String lenstitle = driver.findElement(By.xpath(OrderVariables.lens_title_xpath)).getText();
        return lenstitle;
    }
    public String getDefaultLensPrices() throws Exception{
        String defaultlentsprices = driver.findElement(By.xpath(OrderVariables.default_lens_price_xpath)).getText();
        return defaultlentsprices;
    }
    public boolean isDefaultLensSelected() throws Exception{
        return driver.findElement(By.xpath(OrderVariables.default_lens_tint_xpath)).isSelected();
    }
    public boolean isStandardLensTintSelected() throws Exception{
        return driver.findElement(By.xpath(OrderVariables.standard_lens_tint_radio_xpath)).isSelected();
    }
    public boolean isGradientLensTintSelected() throws Exception{
        return driver.findElement(By.xpath(OrderVariables.gradient_lens_tint_radio_xpath)).isSelected();
    }
    public void selectStandardLensTint(String value) throws Exception{
        Select standlenstintmenu = new Select(driver.findElement(By.xpath(OrderVariables.standard_lens_tint_menu_id)));
        standlenstintmenu.selectByValue(value);
    }
    public void selectGradientLensTint(String value) throws Exception{
        Select gradientlenstint = new Select(driver.findElement(By.xpath(OrderVariables.gradient_lens_tint_menu_id)));
        gradientlenstint.selectByValue(value);
    }
    public void clickLensSubmitButton() throws Exception{
        logger.info("Click next button");
        driver.findElement(By.id(OrderVariables.lens_submit_button_id)).click();
        Thread thread = new Thread();
        thread.sleep(3000);
    }

    /**
     *
     * @param index should start from 2
     * @throws Exception
     */
    public void selectLensCoating(String index) throws Exception{
        driver.findElement(By.xpath("//form[@id = 'addonFormId']/fieldset/div[@class = 'tlist green']/dl["+index+"]/dt/input"));
    }
    public void selectTheFirstAccessoryItem(String quantity) throws Exception{
        Select accessoryitem1menu = new Select(driver.findElement(By.id(OrderVariables.accessory_item_1_menu_id)));
        accessoryitem1menu.selectByValue(quantity);
    }
    public void selectTheSecondAccessoryItem(String quantity) throws Exception{
        Select accessoryitem2menu = new Select(driver.findElement(By.id(OrderVariables.accessory_item_2_menu_id)));
        accessoryitem2menu.selectByValue(quantity);
    }
    public void selectTheThirdAccessoryItem(String quantity) throws Exception{
        Select accessoryitem3menu = new Select(driver.findElement(By.id(OrderVariables.accessory_item_3_menu_id)));
        accessoryitem3menu.selectByValue(quantity);
    }
    public void selectTheFourthAccessoryItem(String quantity) throws Exception{
        Select accessoryitem4menu = new Select(driver.findElement(By.id(OrderVariables.accessory_item_4_menu_id)));
        accessoryitem4menu.selectByValue(quantity);
    }
    public void selectTheFifthAccessoryItem(String quantity) throws Exception{
        Select accessoryitem5menu = new Select(driver.findElement(By.id(OrderVariables.accessory_item_5_menu_id)));
        accessoryitem5menu.selectByValue(quantity);
    }
    public void selectTextOnTheInsideOfTheArm() throws Exception{
        logger.info("Check the checkbox of text on the inside of the arm ");
        if(!driver.findElement(By.id(OrderVariables.text_on_the_inside_of_the_arm_checkbox_id)).isSelected()){
            driver.findElement(By.id(OrderVariables.text_on_the_inside_of_the_arm_checkbox_id)).click();
            //driverUtils.waitTillElementExistsByXpath(driver,OrderVariables.length_label_of_inside_text_xpath,3000);
        }
    }
    public void enterInsideText(String insidetext) throws Exception{
        logger.info("Set the inside text as "+insidetext+" .");
        driver.findElement(By.id(OrderVariables.inside_text_textbox_id)).clear();
        driver.findElement(By.id(OrderVariables.inside_text_textbox_id)).sendKeys(insidetext);
    }
    public void selectInsideTerms() throws Exception{
        logger.info("Check the inside terms checkbox");
        if (!driver.findElement(By.id(OrderVariables.term_inside_text_id)).isSelected()) {
            driver.findElement(By.id(OrderVariables.term_inside_text_id)).click();
        }
    }
    public void selectTextOnTheOutsideOfTheArm() throws Exception{
        logger.info("Check the checkbox of text on the outside of the arm");
        if(!driver.findElement(By.id(OrderVariables.text_on_the_outside_of_the_arm_checkbox_id)).isSelected()){
            driver.findElement(By.id(OrderVariables.text_on_the_outside_of_the_arm_checkbox_id)).click();
        }
    }
    public void enterOutsideText(String outsidetext) throws Exception{
        logger.info("Set the outside text as "+outsidetext+" .");
        driver.findElement(By.id(OrderVariables.outside_text_textbox_id)).clear();
        driver.findElement(By.id(OrderVariables.outside_text_textbox_id)).sendKeys(outsidetext);
    }
    public void setOutsideTextColor(String value) throws Exception{
        logger.info("Set the outside text color as "+value+" .");
        Select outsidetextcolormenu = new Select(driver.findElement(By.id(OrderVariables.outside_text_color_menu_id)));
        outsidetextcolormenu.selectByValue(value);
    }
    public void selectOutsideTextFont(int index) throws Exception{
        driver.findElement(By.xpath("//dd[@class = 'fonts']/span["+index+"]/span/input[1]")).click();
    }
    public void selectOutsideTerms() throws Exception{
        if(!driver.findElement(By.id(OrderVariables.term_outside_text_id)).isSelected()){
            driver.findElement(By.id(OrderVariables.term_outside_text_id)).click();
        }
    }
    public void selectRXQuantity(String value) throws Exception{
        Select rxquantitymenu = new Select(driver.findElement(By.id(OrderVariables.rx_quantity_id)));
        rxquantitymenu.selectByValue(value);
    }
    public void clickAddToCartButton() throws Exception{
        logger.info("Click add to chart button");
        driverUtils.waitTillElementExistsById(driver,OrderVariables.btn_add_to_cart_id);
        driver.findElement(By.id(OrderVariables.btn_add_to_cart_id)).click();
        Thread thread = new Thread();
        thread.sleep(3000);
    }
    public void clickOkInTheWarnPopWindow() throws Exception{
        //driverUtils.waitTillElementExistsByXpath(driver,OrderVariables.warn_message_pop_up_ok_button_xpath,3000);
        logger.info("Click ok button in the warn pop up window");
        driver.findElement(By.xpath(OrderVariables.warn_message_pop_up_ok_button_xpath)).click();
        Thread thread = new Thread();
        thread.sleep(4000);
        //driverUtils.waitTillElementExistsByXpath(driver,OrderVariables.lens_configuration_label_xpath,3000);
    }
    public void clickCancelInTheWarnPopWindow() throws Exception{
        logger.info("Click cancel button in the warn pop up window");
        driverUtils.waitTillElementExistsByXpath(driver,OrderVariables.warn_message_pop_up_cancel_button_xpath,3000);
        driver.findElement(By.xpath(OrderVariables.warn_message_pop_up_cancel_button_xpath)).click();
        Thread thread = new Thread();
        thread.sleep(3000);
    }
    public void clickOkInTheErrorPopWindow() throws Exception{
        driverUtils.waitTillElementExistsByXpath(driver,OrderVariables.error_message_pop_up_ok_button_xpath,3000);
        driver.findElement(By.xpath(OrderVariables.error_message_pop_up_ok_button_xpath)).click();
    }
    public void editPrescriptInfo() throws Exception{
        driver.findElement(By.xpath(OrderVariables.edit_prescriptinfo_link_xpath)).click();
    }
    public void editLensInfo() throws Exception{
        driver.findElement(By.xpath(OrderVariables.edit_lensinfo_link_xpath)).click();
    }
    public void verifySinglevisonisloaded() throws Exception{
        Assert.assertFalse(driver.findElement(By.id(OrderVariables.pdescription_id)).isDisplayed());
    }
    public void closethepopwindow() throws Exception{
        if (driverUtils.isElementExistById(driver, "popup-subcription-backgrounds-container-124d70ff-1403-4327-b91f-cad5f2d24e8a")){
            driver.findElement(By.id("popup-subcription-closes-link-124d70ff-1403-4327-b91f-cad5f2d24e8a")).click();
        }
    }
    public void verifyProgressiveisloaded() throws Exception{
        logger.info("Check the contents of progressive is loaded");
        Assert.assertTrue(driver.findElement(By.id(OrderVariables.pdescription_id)).isDisplayed());
    }
    public void verifyFrameonlyisloaded() throws Exception{
        logger.info("Check the 3rd block is loaded");
        Thread thread = new Thread();
        thread.sleep(3000);
        Assert.assertTrue(driver.findElement(By.id(OrderVariables.btn_add_to_cart_id)).isDisplayed());
    }
    public void verifyPrescriptInfoDisplayed() throws Exception{
        String prescriptInfo = driver.findElement(By.xpath(OrderVariables.prescriptInfo_title_xpath)).getText();
        Assert.assertEquals(prescriptInfo,"PRESCRIPTION");
        Assert.assertTrue(driver.findElement(By.xpath(OrderVariables.importprescription_link_xpath)).isDisplayed());
        Assert.assertTrue(driver.findElement(By.xpath(OrderVariables.clearall_prescription_link_xpath)).isDisplayed());
    }
    public void verifyLensConfiguratorDisplayed() throws Exception{
        String lensconfigurationlabel = driver.findElement(By.xpath(OrderVariables.lens_configuration_label_xpath)).getText();
        Assert.assertEquals(lensconfigurationlabel,"YOUR LENSES");
        String lensconfigurationtips = driver.findElement(By.xpath(OrderVariables.lens_configuration_tips_xpath)).getText();
        Assert.assertEquals(lensconfigurationtips,"Choose the lens options based on your prescription");
        Assert.assertTrue(driver.findElement(By.xpath(OrderVariables.help_me_decide_link_xpath)).isDisplayed());
        Assert.assertTrue(driver.findElement(By.id(OrderVariables.lens_submit_button_id)).isDisplayed());
    }
    public void verifyAddonDisplayed() throws Exception{
        driverUtils.waitTillElementExistsByXpath(driver,OrderVariables.personalization_label,3000);
        String personalizationlabel = driver.findElement(By.xpath(OrderVariables.personalization_label)).getText();
        Assert.assertEquals(personalizationlabel,"CUSTOM ENGRAVING");
        String textontheinsideofthearmlabel = driver.findElement(By.xpath(OrderVariables.text_on_the_inside_of_the_arm_label_xpath)).getText();
        String textontheoutsideofthearmlabel = driver.findElement(By.xpath(OrderVariables.text_on_the_outside_of_the_arm_label_xpath)).getText();
        Assert.assertEquals(textontheinsideofthearmlabel,"Text on the inside of the arm");
        Assert.assertEquals(textontheoutsideofthearmlabel,"Text on the outside of the arm");
    }
    public void verifyTextOnTheInsideOfTheArmIsSelected() throws Exception{
        Assert.assertTrue(driver.findElement(By.id(OrderVariables.text_on_the_inside_of_the_arm_checkbox_id)).isSelected());
        Assert.assertTrue(driver.findElement(By.id(OrderVariables.inside_text_textbox_id)).isDisplayed());
        String lengthlabelofinsidetext = driver.findElement(By.xpath(OrderVariables.length_label_of_outside_text_xpath)).getText();
        //String lengthlabelofoutsidetext = driver.findElement(By.xpath(OrderVariables.length_label_of_outside_text_xpath)).getText();
        Assert.assertEquals(lengthlabelofinsidetext,"(15 letters max)");
    }
    public void verifyTextOnTheOutsideOfTheArmIsSelected() throws Exception{
        Assert.assertTrue(driver.findElement(By.id(OrderVariables.text_on_the_outside_of_the_arm_checkbox_id)).isSelected());
        Assert.assertTrue(driver.findElement(By.id(OrderVariables.outside_text_textbox_id)).isDisplayed());
        //Assert.assertTrue(driver.findElement(By.id(OrderVariables.outside_text_color_menu_id)).isDisplayed());
        //Assert.assertTrue(driver.findElement(By.xpath("//dd[@class = 'fonts']/span[1]/span/input[1]")).isDisplayed());
    }
    public void verifyTextOnTheInsideOfTheArmSubmenusIsNotShow() throws Exception{
        System.out.println(driver.findElement(By.id(OrderVariables.text_on_the_inside_of_the_arm_checkbox_id)).isSelected());
        Assert.assertFalse(driver.findElement(By.id(OrderVariables.text_on_the_inside_of_the_arm_checkbox_id)).isSelected());
        Assert.assertFalse(driver.findElement(By.id(OrderVariables.inside_text_textbox_id)).isDisplayed());
    }
    public void verifyTextOnTheOutsideOfTheArmSubmenusIsNotShow() throws Exception{
        Assert.assertFalse(driver.findElement(By.id(OrderVariables.text_on_the_outside_of_the_arm_checkbox_id)).isSelected());
        Assert.assertFalse(driver.findElement(By.id(OrderVariables.outside_text_textbox_id)).isDisplayed());
    }
    public void verifyContentsOfWarnPopWindow() throws Exception{
        String popUpTitle = driver.findElement(By.xpath(OrderVariables.warn_message_pop_up_title_xpath)).getText();
        Assert.assertEquals(popUpTitle,"Are you sure?");
        String popUpContents = driver.findElement(By.xpath(OrderVariables.warn_message_contens_xpath)).getText();
        Assert.assertEquals(popUpContents,"All of the values are 0. Do you want to order lenses without a prescription? Please confirm your prescription details.");
    }
    public void verifyContentsofErrorPopWindow() throws Exception{
        String popUpTitle = driver.findElement(By.xpath(OrderVariables.error_message_pop_up_title_xpath)).getText();
        Assert.assertEquals(popUpTitle,"Error");
        String popUpContents = driver.findElement(By.xpath(OrderVariables.error_message_pop_up_contents_xpath)).getText();
        Assert.assertEquals(popUpContents,"Please select a prescription type.");
    }
}
